local zhuandui = fk.CreateSkill {

  name = "joy__zhuandui",

  tags = {  },

}



zhuandui:addEffect(fk.TargetSpecified, {
  name = "joy__zhuandui",
  mute = true,
  events ={fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhuandui.name) and data.card.trueName == "slash" and not player:isKongcheng() then
      if event == fk.TargetSpecified then
        return data.to ~= player.id and not player.room:getPlayerById(data.to):isKongcheng()
      else
        return data.from ~= player.id and not player.room:getPlayerById(data.from):isKongcheng()
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local prompt
    if event == fk.TargetSpecified then
      prompt = "joy__zhuandui1-invoke::"..data.to..":"..data.card:toLogString()
    else
      prompt = "joy__zhuandui2-invoke::"..data.from..":"..data.card:toLogString()
    end
    return player.room:askForSkillInvoke(player, zhuandui.name, nil, prompt)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(zhuandui.name)
    local win = false
    if event == fk.TargetSpecified then
      room:notifySkillInvoked(player, zhuandui.name, "offensive")
      room:doIndicate(player.id, {data.to})
      local pindian = player:pindian({room:getPlayerById(data.to)}, zhuandui.name)
      if pindian.results[data.to].winner == player then
        win = true
        data.disresponsiveList = data.disresponsiveList or {}
        table.insert(data.disresponsiveList, data.to)
      end
    else
      room:notifySkillInvoked(player, zhuandui.name, "defensive")
      room:doIndicate(player.id, {data.from})
      local pindian = player:pindian({room:getPlayerById(data.from)}, zhuandui.name)
      if pindian.results[data.from].winner == player then
        win = true
        table.insertIfNeed(data.nullifiedTargets, player.id)
      end
    end
    if win and not player.dead then
      player:drawCards(1,zhuandui.name)
    end
  end,
})
zhuandui:addEffect(fk.TargetConfirmed, {
  name = "joy__zhuandui",
  mute = true,
  events ={fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhuandui.name) and data.card.trueName == "slash" and not player:isKongcheng() then
      if event == fk.TargetSpecified then
        return data.to ~= player.id and not player.room:getPlayerById(data.to):isKongcheng()
      else
        return data.from ~= player.id and not player.room:getPlayerById(data.from):isKongcheng()
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local prompt
    if event == fk.TargetSpecified then
      prompt = "joy__zhuandui1-invoke::"..data.to..":"..data.card:toLogString()
    else
      prompt = "joy__zhuandui2-invoke::"..data.from..":"..data.card:toLogString()
    end
    return player.room:askForSkillInvoke(player, zhuandui.name, nil, prompt)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(zhuandui.name)
    local win = false
    if event == fk.TargetSpecified then
      room:notifySkillInvoked(player, zhuandui.name, "offensive")
      room:doIndicate(player.id, {data.to})
      local pindian = player:pindian({room:getPlayerById(data.to)}, zhuandui.name)
      if pindian.results[data.to].winner == player then
        win = true
        data.disresponsiveList = data.disresponsiveList or {}
        table.insert(data.disresponsiveList, data.to)
      end
    else
      room:notifySkillInvoked(player, zhuandui.name, "defensive")
      room:doIndicate(player.id, {data.from})
      local pindian = player:pindian({room:getPlayerById(data.from)}, zhuandui.name)
      if pindian.results[data.from].winner == player then
        win = true
        table.insertIfNeed(data.nullifiedTargets, player.id)
      end
    end
    if win and not player.dead then
      player:drawCards(1,zhuandui.name)
    end
  end,
})

return zhuandui